package cn.mumzone.basic.loading.button;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import cn.mumzone.basic.R;
import cn.mumzone.basic.loading.dialog.LoadingIndicatorView;

/**
 * Created by JoeLjt on 2019/6/26.
 * Email: lijiateng1219@gmail.com
 * Description: 带 Loading 效果的按钮
 */

public class LoadingButton extends LinearLayout {

    private static final int TEXT_DIFF = 14;
    View mRootView;
    private int textColor;
    private String text;
    private int backgroundColor;
    private int loadingColor;
    private Drawable background;
    private Drawable drawableLeft;
    private int textSize;
    private TextView mTextButton;
    private LoadingIndicatorView mLoadingView;
    private View[] unEnableViewList;
    private boolean isEnable = true;

    public LoadingButton(Context context) {
        super(context);
        init(context);
    }

    public LoadingButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        getAttributes(context, attrs);
        init(context);
    }

    public LoadingButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        getAttributes(context, attrs);
        init(context);
    }

    private void getAttributes(Context context, AttributeSet attrs) {
        TypedArray ta = context.getTheme().obtainStyledAttributes(attrs, R.styleable.LoadingButton, 0, 0);
        this.text = ta.getString(R.styleable.LoadingButton_text);
        this.textColor = ta.getColor(R.styleable.LoadingButton_textColor, getResources().getColor(android.R.color.black));
        this.textSize = ta.getDimensionPixelOffset(R.styleable.LoadingButton_textSize, 0);
        this.backgroundColor = ta.getColor(R.styleable.LoadingButton_backgroundColor, 0);
        this.loadingColor = ta.getColor(R.styleable.LoadingButton_loadingColor, 0);
        this.background = ta.getDrawable(R.styleable.LoadingButton_background);
        this.drawableLeft = ta.getDrawable(R.styleable.LoadingButton_drawableLeft);
        this.isEnable = ta.getBoolean(R.styleable.LoadingButton_isEnable, true);

        ta.recycle();
    }

    private void init(Context context) {

        setOrientation(HORIZONTAL);
        setMinimumWidth(dip2px(context, 100));
        setMinimumHeight(dip2px(context, 40));
        setGravity(Gravity.CENTER);
        setPadding(dip2px(context, 15), 0, dip2px(context, 15), 0);

        this.mRootView = inflate(context, R.layout.button_loading, this);
        this.mLoadingView = mRootView.findViewById(R.id.loading);
        this.mTextButton = mRootView.findViewById(R.id.button);

        if (!TextUtils.isEmpty(text))
            mTextButton.setText(text.toUpperCase());

        if (textColor != 0)
            mTextButton.setTextColor(textColor);

        if (loadingColor != 0)
            mLoadingView.setIndicatorColor(loadingColor);

        if (textSize != 0)
            mTextButton.setTextSize(textSize - (textSize - TEXT_DIFF));

        if (backgroundColor != 0)
            this.mRootView.setBackgroundColor(backgroundColor);

        if (background != null)
            this.mRootView.setBackground(background);

        if (drawableLeft != null)
            drawableLeft.setBounds(0, 0, drawableLeft.getMinimumWidth(), drawableLeft.getMinimumHeight());
        mTextButton.setCompoundDrawablesRelative(drawableLeft, null, null, null);

        mRootView.setEnabled(isEnable);

    }

    public void startLoading(String loadingText) {
        this.setClickable(false);
        this.mLoadingView.setVisibility(VISIBLE);
        this.mTextButton.setText(loadingText);
    }

    public void startLoading(String loadingText, View... views) {
        this.unEnableViewList = views;
        this.setClickable(false);
        this.mLoadingView.setVisibility(VISIBLE);
        this.mTextButton.setText(loadingText);
        if (unEnableViewList != null && unEnableViewList.length > 0) {
            for (View view : unEnableViewList) {
                view.setClickable(false);
            }
        }
    }

    public void stopLoading(String loadingDoneText) {
        this.setClickable(true);
        this.mLoadingView.setVisibility(GONE);
        this.mTextButton.setText(loadingDoneText);
        if (unEnableViewList != null && unEnableViewList.length > 0) {
            for (View view : unEnableViewList) {
                view.setClickable(true);
            }
        }
    }

    public int getTextColor() {
        return textColor;
    }

    public void setTextColor(int textColor) {
        this.textColor = textColor;
        mTextButton.setTextColor(textColor);
    }

    public String getText() {
        return text;
    }

    public void setText(@NonNull String text) {
        this.text = text;
        mTextButton.setText(text);
    }

    public int getCustomBackgroundColor() {
        return backgroundColor;
    }

    public void setCustomBackgroundColor(int backgroundColor) {
        this.backgroundColor = backgroundColor;
        mRootView.setBackgroundColor(backgroundColor);
    }

    public Drawable getCustomBackground() {
        return background;
    }

    public void setCustomBackground(@NonNull Drawable background) {
        this.background = background;
        mRootView.setBackground(background);
    }

    public int getTextSize() {
        return textSize;
    }

    public void setTextSize(int textSize) {
        this.textSize = textSize;
        mTextButton.setTextSize(textSize - (textSize - TEXT_DIFF));
    }

    public boolean isEnable() {
        return isEnable;
    }

    public void setEnable(boolean enable) {
        isEnable = enable;
        mRootView.setEnabled(isEnable);
    }

    public int dip2px(Context context, double dpValue) {
        float density = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * density + 0.5);
    }

}
